package actions;

import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import utils.DBSelect;
import utils.DBSelectInt;
import utils.JSONResponse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassroomsAction extends CMActionSupport {
	private Logger logger = LogManager.getLogger(MaintowerDeformationAction.class);
	//查询信息
	private String date; //日期
	private String time; //时间段
	private String value; //容量
	private String state; //状态
	//点击预约
	private String classroomId; // 教室编号
	private String userId; // 用户id
	private String reason; //预约理由

	//高级管理员
	private String classroom_id;
	private String building;
	private String room;
	private String classroom_capacity;
	private String multimedia;

	public String execute() {
		//判断是查询还是预约
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();
		if (userId == null || userId.equals("")) { //如果没有上传usersId说明是 查询数据
			String sql = "select * from classroom";
			new DBSelect(sql).exec(new DBSelectInt() {

				@Override
				public boolean deal(ResultSet rs) throws SQLException {
					while (rs.next()) {
						JSONObject tmpJson = new JSONObject();
						tmpJson.put("classroomId", rs.getString("building") + "-" + rs.getString("room"));
						tmpJson.put("date", "所有时间");
						tmpJson.put("value", rs.getString("capacity"));
						tmpJson.put("state", "可用");

						jsonList.add(tmpJson);
					}
					return false;
				}
			});
			result.put("data", jsonList);
			JSONResponse.returnJsonToResponse(result);
			return NONE;
		} else {
			/*procedure `borrow`(`_borrowdate` date, `_borrowtime` enum('1', '2', '3'), `_userid` char(8), `_reason` text)
			 */

			String sql = "borrow"; //查找某个id的待审核数据
			/*存储过程没写classroomid 所以这里先用sql语句写死了*/
			sql = "insert into borrowlog (classroomid, borrowdate, borrowtime, userid, reason) values(" +
					"'" + classroomId + "', '" + date + "', '" + time + "', '" + userId + "', '" + reason +
					"');";

			new DBSelect(sql).exec(new DBSelectInt() {
				@Override
				public boolean deal(ResultSet rs) throws SQLException {
					return false;
				}
			});

//			new DBSelect(sql, date, time, userId, reason).execProcedure(
//					new DBSelectInt() {
//
//						@Override
//						public boolean deal(ResultSet rs) throws SQLException {
//
//							return false;
//						}
//					});
			result.put("data", "预约成功");
			JSONResponse.returnJsonToResponse(result);
			return NONE;
		}
	}
	public String list(){
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();
		String sql = "select * from classroom"; //查看所有教室

		new DBSelect(sql).exec(new DBSelectInt() {

			@Override
			public boolean deal(ResultSet rs) throws SQLException {
				while (rs.next()) {
					JSONObject tmpJson = new JSONObject();
					tmpJson.put("classroom_id",rs.getInt("id"));
					tmpJson.put("building",rs.getString("building"));
					tmpJson.put("room",rs.getString("room"));
					tmpJson.put("classroom_capacity",rs.getInt("capacity"));
					int multimedia = rs.getInt("multimedia");
					String confirm;
					switch (multimedia){
						case 0:
							confirm = "否";
							break;
						case 1:
							confirm = "是";
							break;
						default:confirm="读取出错";
					}
					tmpJson.put("multimedia",confirm);
					jsonList.add(tmpJson);
				}
				return false;
			}
		});
		result.put("data",jsonList);
		JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String add() {
		String confirm = "0";
		if (multimedia.equals("是")){
			confirm = "1";
		}
		String sql = "insert into classroom(building,room,multimedia,capacity) VALUES('"+building+"','"+room+"','"+confirm+"','"+classroom_capacity+"')";
		new DBSelect(sql).exec(new DBSelectInt() {

			@Override
			public boolean deal(ResultSet rs) throws SQLException {
				return false;
			}
		});
//		JSONObject result=new JSONObject();
//        result.put("status","success");
//        JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String del() {
		String sql = "delete from classroom where id='"+classroom_id+"'";
		new DBSelect(sql).exec(new DBSelectInt() {

			@Override
			public boolean deal(ResultSet rs) throws SQLException {
				return false;
			}
		});
        JSONObject result=new JSONObject();
        result.put("status","success");
        JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String getClassroom_id() {
		return classroom_id;
	}

	public void setClassroom_id(String classroom_id) {
		this.classroom_id = classroom_id;
	}

	public String getBuilding() {
		return building;
	}

	public void setBuilding(String building) {
		this.building = building;
	}

	public String getRoom() {
		return room;
	}

	public void setRoom(String room) {
		this.room = room;
	}

	public String getClassroom_capacity() {
		return classroom_capacity;
	}

	public void setClassroom_capacity(String classroom_capacity) {
		this.classroom_capacity = classroom_capacity;
	}

	public String getMultimedia() {
		return multimedia;
	}

	public void setMultimedia(String multimedia) {
		this.multimedia = multimedia;
	}
	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getClassroomId() {
		return classroomId;
	}

	public void setClassroomId(String classroomId) {
		this.classroomId = classroomId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}
}
